using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LeisureCenter.Api;
using LeisureCenter.Controls;

namespace LeisureCenter.Forms
{
    public partial class DialogForm : Form
    {
        private IDialogControl dialogControl;

        public DialogForm()
        {
            InitializeComponent();
        }

        public static bool Execute(IDialogControl control)
        {
            Form background = Program.LeisureCenterInstance.UIManager.CreateAlphaBackgroundForm(control.AlphaBackgroundColor);
            using (background)
            {
                background.Show();
                try
                {
                    DialogForm form = new DialogForm();
                    using (form)
                    {
                        form.dialogControl = control;
                        form.Width = control.Control.Width + (form.pnlControl.Left + form.pnlControl.Padding.All) * 2;
                        form.Height = control.Control.Height + (form.pnlControl.Top + form.pnlControl.Padding.All) * 2 + form.pnlButtons.Height;
                        form.pnlControl.FillColor = control.Control.BackColor;
                        control.Control.Dock = DockStyle.Fill;
                        form.pnlControl.Controls.Add(control.Control);

                        form.btnOk.Visible = control.ShowOkButton;
                        form.btnCancel.Visible = control.ShowCancelButton;

                        if (!string.IsNullOrEmpty(control.Title))
                        {
                            form.pnlTitle.Visible = true;
                            form.lblTitle.Text = control.Title;
                            Graphics g = Graphics.FromHwnd(form.lblTitle.Handle);
                            using (g)
                            {
                                form.pnlTitle.Height = (int)g.MeasureString(control.Title, form.lblTitle.Font, form.lblTitle.Width).Height + form.pnlTitle.Padding.Top + form.pnlTitle.Padding.Bottom;
                            }
                            form.Height += form.pnlTitle.Height;
                        }

                        control.SetFocusHandlers(form.btnOk, form.btnCancel);
                        form.btnOk.FocusUp = control.FocusHandler;
                        form.btnCancel.FocusUp = control.FocusHandler;

                        bool result = form.ShowDialog() == DialogResult.OK;
                        return result;
                    }
                }
                finally
                {
                    background.Hide();
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dialogControl == null || this.dialogControl.OnOk())
                DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.dialogControl == null || this.dialogControl.OnCancel())
                DialogResult = DialogResult.Cancel;
        }
    }
}